package github.remonlong.restfulutil.exception.util;

import java.util.HashMap;
import java.util.Map;

/**
 * map 工具类
 *
 * @author remon
 * @create 2018-09-18 21:39
 **/
public class M<K, V> {

    private static M m;

    private Map<K, V> map = new HashMap<K, V>();

    private M(K key, V value) {
        add(key, value);
    }

    private M() {

    }

    public static M ins() {

        return ins(null, null);
    }

    public static <K, V> M ins(K k, V v) {

        if (k != null) {
            if (k instanceof String) {
                String kStr = (String) k;
                if ("".equals(kStr)) {
                    m = new M();
                }
            } else if (m == null) {
                m = new M(k, v);
            }
            m = new M(k, v);
        } else {
            m = new M();
        }
        return m;
    }

    public M add(K key, V value) {
        if (value != null)
            map.put(key, value);
        return this;
    }

    public M addNull(K key, V value) {
        map.put(key, value);
        return this;
    }

    public Map<K, V> map() {
        return this.map;
    }

}
